package communicationMessages;

import java.util.logging.Level;

import serverComuncation.Observer;

import logger.LoggerWriter;
import data.MemberDataBase;
import database.DB;

public class LoginMessage extends Message {

	@Override
	public String getAnswer() {
		String ans;
		String userName,password;
		LoggerWriter logger = LoggerWriter.getLoggerWriter();
		String arr[] = this.getContent().split(delimiter);
		if (arr.length == 2)
		{
			userName = arr[0];
			password = arr[1];
		}
		else
		{
			ans = "wrong format"+delimiter;
			logger.log("wrong format login msg", Level.WARNING);
			return ans;
		}
		if (DB.authenticate(userName,password))
		{
			String userRoll = DB.getUserRoll(userName);
			ans="login"+delimiter+ userRoll +delimiter+"success"+delimiter;
			logger.log("sucsses login " + userName,Level.INFO);
			if (DB.isActiveUser(userName))
			{
				this.activateUser(userName);
			}
		}
		else
		{
			logger.log("failed user name password " + userName, Level.INFO);
			ans = "login"+delimiter+"guest"+delimiter+"failed"+delimiter+"wrong user or password"+delimiter;
		}
		/*
		MemberDataBase uDataBase = MemberDataBase.getMembersDataBase();
		if (uDataBase.authenticate(userName, password))
		{
			
			String userRoll = MemberDataBase.getUserRoll(userName);
			ans="login"+delimiter+ userRoll +delimiter+"success"+delimiter;
			logger.log("sucsses login " + userName,Level.INFO);
			uDataBase.addLoginMember(uDataBase.getMember(userName));
		}
		else
		{
			logger.log("failed user name password " + userName, Level.INFO);
			ans = "login"+delimiter+"guest"+delimiter+"failed"+delimiter+"wrong user or password"+delimiter;
		}*/
		return ans;
	}

}
